package com.goods.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.goods.mapper.BookMapper;
import com.goods.pojo.Book;
import com.goods.pojo.Category;
import com.goods.pojo.Page;
import com.goods.pojo.PageConstants;
import com.goods.service.BookService;
@Service
public class BookServiceImpl implements BookService{
	
	@Resource
	private BookMapper bookMapper;

	// 按分类查
	@Override
	public Page<Book> findByCategory(String cid, int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		//获取总记录数
		int tr = bookMapper.Count(cid, null, null, null);
		// 计算当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		List<Book> beanList = bookMapper.find(cid, null, null, null, pc1, ps);
		Page<Book> pb = new Page<Book>();
		pb.setBeanList(beanList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}
	// 按书名查
	@Override
	public Page<Book> findByBname(String bname, int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		//获取中记录数
		int tr = bookMapper.Count(null, bname, null, null);
		//当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		List<Book> beanList = bookMapper.find(null, bname, null, null, pc1, ps);
		Page<Book> pb = new Page<Book>();
		pb.setBeanList(beanList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}
	// 按作者查
	@Override
	public Page<Book> findByAuthor(String author, int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		// 获取中记录数
		int tr = bookMapper.Count(null, null, author, null);
		// 当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		
		List<Book> beanList = bookMapper.find(null, null, author, null, pc1, ps);
		Page<Book> pb = new Page<Book>();
		pb.setBeanList(beanList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}
	// 按出版社查
	@Override
	public Page<Book> findByPress(String press, int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		// 获取中记录数
		int tr = bookMapper.Count(null, null, null, press);
		// 当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		
		List<Book> beanList = bookMapper.find(null, null, null, press, pc1, ps);
		Page<Book> pb = new Page<Book>();
		pb.setBeanList(beanList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}

	// 多条件组合查询
	@Override
	public Page<Book> findByCombination(Book criteria, int pc) {

		int ps = PageConstants.BOOK_PAGE_SIZE;//每页记录数
		//获取中记录数
		int tr = bookMapper.Count(null, criteria.getBname(), criteria.getAuthor(), criteria.getPress());
		//当前页首行记录的下标
		int pc1 = (pc-1) * ps;
		
		List<Book> beanList = bookMapper.find(null, criteria.getBname(), criteria.getAuthor(), criteria.getPress(), pc1, ps);
		Page<Book> pb = new Page<Book>();
		pb.setBeanList(beanList);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}
	
	// 查询一本书的分类
	@Override
	public Book findByBid(String bid) {
		Book book = bookMapper.findByBid(bid);
		book.setCategory(bookMapper.findByCid(book.getCid()));
		return book;
	}

	// 查询该类别下书籍总数
	@Override
	public int findBookCountByCategory(String cid) {
		return bookMapper.Count(cid,null,null,null);
	}

	// 根据分类ID查询类别
	@Override
	public Category findCategoryByCid(String cid) {
		return bookMapper.findByCid(cid);
	}

	@Override
	public int add(Book book) {
		return bookMapper.add(book);
	}

	@Override
	public int delete(String bid) {
		return bookMapper.delete(bid);
	}

	@Override
	public int edit(Book book) {
		return bookMapper.edit(book);
	}
	
}
